home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / circuits / irsim_ta.z / irsim_ta / irsim / src / ana11 / textwind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  5.7 KB  |  272 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <varargs.h>
  17. #include "ana.h"
  18. #include "ana_glob.h"
  19. #include "graphics.h"
  20.  
  21.  
  22. #define    BUFFSIZE    256
  23.  
  24.  
  25. private char    buffer[ BUFFSIZE ];    /* text widow buffer */
  26. private int     txtPos = 0;        /* postion to print next character */
  27. private    char    txt_cursor[] = " ";    /* cursor: a space in inverse video */
  28. private    int     cursor_on = FALSE;    /* TRUE when cursor on */
  29. private    int     maxPos;
  30.  
  31.  
  32. #define    XPOS( Pos )        ( ((Pos) * CHARWIDTH) + 2 )
  33. #define    YPOS            ( textBox.bot - 1 - descent )
  34. #define    CWIDTH( X )        ( (X) * CHARWIDTH )
  35. #define    TXT_HEIGHT        ( textBox.bot - textBox.top )
  36.  
  37.  
  38. #define    PTEXT( Text, N, Len )            XDrawImageString \
  39.     ( display, window, gcs.black, XPOS( N ), YPOS, Text, Len )
  40.  
  41. #define    PCURSOR()                XDrawImageString \
  42.     ( display, window, gcs.white, XPOS( txtPos ), YPOS, txt_cursor, 1 )
  43.  
  44. #define    ERASE_CURSOR()                            \
  45.     PTEXT( txt_cursor, txtPos, 1 )
  46.  
  47.  
  48. public void RedrawText()
  49.   {
  50.     maxPos = textBox.right / CHARWIDTH;
  51.     if( maxPos > BUFFSIZE )
  52.     maxPos = BUFFSIZE;
  53.     FillBox( window, textBox, gcs.white );
  54.     HLine( window, textBox.left, textBox.right, textBox.top, gcs.black );
  55.     if( txtPos )
  56.     PTEXT( buffer, 0, txtPos );
  57.     if( cursor_on )
  58.     PCURSOR();
  59.   }
  60.  
  61.  
  62. public void PRINT( s )
  63.   char  *s;
  64.   {
  65.     int  len;
  66.  
  67.     if( *s == '\n' )
  68.       {
  69.     if( txtPos > 0 )
  70.         FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
  71.          TXT_HEIGHT, gcs.white );
  72.     txtPos = 0;
  73.     s++;
  74.       }
  75.     len = strlen( s );
  76.     if( txtPos + len >= BUFFSIZE )
  77.       {
  78.     len = BUFFSIZE - 1 - txtPos;
  79.       }
  80.     if( len > 0 )
  81.       {
  82.     bcopy( s, buffer + txtPos, len );
  83.     PTEXT( s, txtPos, len );
  84.     txtPos += len;
  85.       }
  86.   }
  87.  
  88.  
  89. public void PRINTF( va_alist )
  90.   va_dcl
  91.   {
  92.     va_list  args;
  93.     char     *format;
  94.     char     *s;
  95.     int      len;
  96.  
  97.     va_start( args );
  98.     format = va_arg( args, char * );
  99.  
  100.     if( *format == '\n' )
  101.       {
  102.     if( txtPos > 0 )
  103.         FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
  104.          TXT_HEIGHT, gcs.white );
  105.     txtPos = 0;
  106.     format++;
  107.     s = buffer;
  108.       }
  109.     else
  110.     s = buffer + txtPos;
  111.  
  112.     vsprintf( s, format, args );
  113.     len = strlen( s );
  114.     PTEXT( s, txtPos, len );
  115.     txtPos += len;
  116.   }
  117.  
  118.  
  119. private    Func    FQuerying;
  120. private    char    *inpStart;
  121. private    void    StrInput();
  122.  
  123.  
  124. public void Query( prompt, func )
  125.   char   *prompt;
  126.   Func   func;
  127.   {
  128.     PRINT( prompt );
  129.     inpStart = buffer + txtPos;
  130.     cursor_on = TRUE;
  131.     FQuerying = func;
  132.     PCURSOR();
  133.     SendEventTo( StrInput );
  134.   }
  135.  
  136.  
  137.  
  138.     /* define some interesting control characters */
  139.  
  140. #define    BACKSPACE    '\b'
  141. #define DELETE        '\177'
  142. #define    TAB        '\t'
  143. #define    RETURN        '\r'
  144. #define    NEWLINE        '\n'
  145. #define    CTRL_C        '\003'
  146. #define    CTRL_U        '\025'
  147. #define    CTRL_W        '\027'
  148.  
  149.  
  150. private int Cancel()
  151.   {
  152.     int  left;
  153.  
  154.     bcopy( "(canceled)", inpStart, 11 );
  155.     left = XPOS( inpStart - buffer + 10 );
  156.     FillAREA( window, left, textBox.top + 1, textBox.right - left + 1,
  157.      TXT_HEIGHT, gcs.white );
  158.     txtPos = inpStart - buffer;
  159.     inpStart = NULL;
  160.     return( txtPos + 10 );
  161.   }
  162.  
  163.  
  164. private void StrInput( ev )
  165.   XKeyEvent  *ev;
  166.   {
  167.     char           buff[ 40 ];
  168.     register char  *iPtr, *p;
  169.     register int   newPos, len;
  170.     int            nChars, finish = FALSE;
  171.  
  172.     if( ev == NULL )
  173.       {
  174.     cursor_on = FALSE;
  175.     ERASE_CURSOR();
  176.     txtPos = 0;
  177.     (*FQuerying)( NULL );
  178.     return;
  179.       }
  180.     
  181.     if( ev->type != KeyPress )
  182.       {
  183.     XBell( display, 0 );
  184.     return;
  185.       }
  186.  
  187.     newPos = txtPos;
  188.     iPtr = buffer + newPos;
  189.     nChars = XLookupString( ev, buff, sizeof( buff ), NULL, NULL );
  190.  
  191.     for( p = buff, len = nChars; len > 0 and not finish; p++, len-- )
  192.       {
  193.         switch( *p )
  194.       {
  195.         case BACKSPACE :
  196.         case DELETE :
  197.         if( iPtr > inpStart )
  198.           {
  199.             newPos--;
  200.             iPtr--;
  201.           }
  202.         break;
  203.         case RETURN :
  204.         case NEWLINE :
  205.         *iPtr = '\0';
  206.         finish = TRUE;
  207.         break;
  208.  
  209.         case TAB :
  210.         *iPtr++ = ' ';
  211.         newPos++;
  212.         break;
  213.  
  214.         case CTRL_C :
  215.         newPos = Cancel();
  216.         finish = TRUE;
  217.         break;
  218.  
  219.         case CTRL_U :
  220.         if( iPtr > inpStart )
  221.           {
  222.             newPos = inpStart - buffer;
  223.             iPtr = inpStart;
  224.           }
  225.          break;
  226.  
  227.         case CTRL_W :
  228.         if( iPtr > inpStart )
  229.           {
  230.             iPtr--;
  231.             while( iPtr > inpStart and *iPtr == ' ' )
  232.             iPtr--;
  233.             while( iPtr > inpStart and *iPtr != ' ' )
  234.             iPtr--;
  235.             if( iPtr != inpStart )
  236.             iPtr++;
  237.             newPos = iPtr - buffer;
  238.           }
  239.         break;
  240.         default :
  241.         if( *p >= ' ' )        /* displayable character */
  242.           {
  243.             if( newPos < maxPos )
  244.               {
  245.             *iPtr++ = *p;
  246.             newPos++;
  247.               }
  248.             else
  249.             XBell( display, 0 );
  250.           }
  251.       }
  252.       }
  253.  
  254.     if( newPos < txtPos )
  255.     FillAREA( window, XPOS( newPos ), textBox.top + 1, 
  256.      CWIDTH( txtPos - newPos + 1 ), TXT_HEIGHT, gcs. white );
  257.     else if( newPos > txtPos )
  258.     PTEXT( buffer + txtPos, txtPos, newPos - txtPos );
  259.  
  260.     txtPos = newPos;
  261.  
  262.     if( finish )
  263.       {
  264.     SendEventTo( NULL );
  265.     cursor_on = FALSE;
  266.     ERASE_CURSOR();
  267.     (*FQuerying)( inpStart );
  268.       }
  269.     else
  270.     PCURSOR();
  271.   }
  272.